home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / t_os / chtiff / source / chtiff8.c < prev    next >
Text File  |  1993-07-08  |  9KB  |  340 lines

  1. /*
  2. このファイルはchtiff5.cがDOS用なために、UNIX用として改造したものです。
  3. 基本的にはファイルをいじる所以外は、変更点はありません。
  4. */
  5.  
  6. /*
  7. ファイル名指定を良くするために、DOS専用関数 _splitpath を使用してます。
  8. MS-C のみに通用する関数かもしれませんので、代用として、__splitpath を用意
  9. してます。信頼度は_splitpath の方が上でしょう。
  10. */
  11.  
  12. #include<stdio.h>
  13. #include<stdlib.h>
  14. #include<ctype.h>
  15. #include<string.h>
  16. #include"define.h"
  17. #define MAIN 1
  18. #include"function.h"
  19.  
  20.  
  21. /* グローバル変数の宣言 */
  22. TIFF_INFO from,to;
  23. int kill_to;
  24.  
  25. void __splitpath(char *string, char *dir, char *fname)
  26. {
  27.     char *p;
  28.     int i;
  29.  
  30.     *dir = *fname = 0;
  31.  
  32.     while(1){
  33.         for( p = string; *p && (*p != '/'); p++);
  34.         if( *p == 0 ) break;
  35.         while( string != p ){
  36.             *dir = *string;
  37.             dir++;
  38.             string++;
  39.             }
  40.         *dir = *string;
  41.         dir++;
  42.         string++;
  43.         }
  44.  
  45.     *dir = 0;
  46.  
  47.     strcpy( fname, string );
  48. }
  49.  
  50.  
  51.  
  52. main(int argc, char *argv[])
  53. {
  54.     unsigned long offset;
  55.     int i, j, from_file, to_file, del, Intel, stop, expand;
  56.     char string[2][256];
  57.     char dir[2][256];
  58.     char fname[2][256];
  59.     char ren[256];
  60.  
  61.     from_file = to_file = 0;
  62.     expand = 0;
  63.     kill_to = 0;
  64.     to.Compression = 1;
  65.     del = 0;
  66.     stop = 1;
  67.     Intel = 2;
  68.     to.Bits_Per_Sample = 0;
  69.     to.Rows_Per_Strip = 0;
  70.     to.X_Resolution.bunsi = to.Y_Resolution.bunsi = 0UL;
  71.     to.X_Resolution.bunbo = to.Y_Resolution.bunbo = 1UL;
  72.     ren[0] = 0;
  73.     string[0][0] = string[1][0] = 0;
  74.  
  75.     for(i=1; i<argc; i++){
  76.         if( (*argv[i]=='/')||(*argv[i]=='-') ) goto OPTION;
  77.  
  78.         if( from_file == 0 ){ from_file = i; continue;}
  79.         if( to_file == 0 ) to_file = i;
  80.         continue;    /* 3つ以上のファイル指定は無視 */
  81.  
  82.         OPTION:
  83.         switch( *(argv[i]+1) ){
  84.             case'c': case'C':
  85.                 to.Compression = 5;
  86.                 continue;
  87.  
  88.             case'd': case'D':
  89.                 switch( *(argv[i]+2) ){
  90.                     case'x': case'X':
  91.                         to.X_Resolution.bunsi = atol( argv[i]+3 );
  92.                         continue;
  93.                     case'y': case'Y':
  94.                         to.Y_Resolution.bunsi = atol( argv[i]+3 );
  95.                         continue;
  96.                     default:
  97.                         to.X_Resolution.bunsi = to.Y_Resolution.bunsi =
  98.                         atol( argv[i]+2);
  99.                         }
  100.                 continue;
  101.  
  102.             case'b': case'B':
  103.                 j = atoi( argv[i]+2 );
  104.                 if( ( j%8 )||( j<8 )||( 24<j) ){
  105.                     printf("この変換には対応してません。%d",j);
  106.                     usage();
  107.                     exit(1);
  108.                     }
  109.                 to.Bits_Per_Sample = j;
  110.                 continue;
  111.  
  112.             case'i': case'I':
  113.                 Intel = 1;
  114.                 continue;
  115.  
  116.             case'm': case'M':
  117.                 Intel = 0;
  118.                 continue;
  119.  
  120.             case's': case'S':
  121.                 j = atoi( argv[i]+2 );
  122.                 if(j<=0){
  123.                     puts("Sオプション指定が間違ってます。");
  124.                     usage();
  125.                     exit(1);
  126.                     }
  127.                 to.Rows_Per_Strip = j;
  128.                 continue;
  129.  
  130.             case'n': case'N':
  131.                 stop = 0;
  132.                 continue;
  133.  
  134.             case'e': case'E':
  135.                 expand = 1;
  136.                 continue;
  137.  
  138.             default:
  139.                 usage();
  140.                 exit(0);
  141.             }
  142.         }
  143.  
  144.     /* ファイル名設定なしなどに対応する。*/
  145.     if( from_file == 0 ){
  146.         fputs("変換元ファイル名が、指定されてません\n",stderr);
  147.         usage();
  148.         exit(1);
  149.         }
  150.     __splitpath( argv[ from_file], dir[0], fname[0] );
  151.     if( *fname[0] == 0 ){
  152.         fputs("変換元ファイル名が、指定されてません\n",stderr);
  153.         usage();
  154.         exit(1);
  155.         }
  156.  
  157.     /* 拡張子省略・変換先設定なしなどに対応 */
  158.     if( to_file == 0 ){
  159.         sprintf(ren,"./%s",fname[0]);    /* 同名をカレントに作成 */
  160.         strcpy(string[1],ren);
  161.         strcat(string[1],".@");
  162.         }
  163.     else{
  164.         __splitpath( argv[to_file], dir[1], fname[1] );
  165.         if( *fname[1] == 0 ){
  166.             sprintf(ren,"%s%s",dir[1],fname[0]);
  167.             strcpy(string[1],ren);
  168.             strcat(string[1],".@");
  169.             }
  170.         else{
  171.             strcpy( ren, argv[to_file] );
  172.             strcpy( string[1],ren );
  173.             strcat( string[1],".@" );
  174.             }
  175.         }        
  176.  
  177.     if( NULL==(from.fp=fopen(argv[from_file],"rb")) ){
  178.         fprintf(stderr,"変換元 %s が見つかりません。\n",argv[from_file]);
  179.         usage();
  180.         exit(1);
  181.         }
  182.  
  183.     if( isIntel() == -1 ){
  184.         fprintf(stderr,"%sは TIFF file ではないようです。",argv[from_file]);
  185.         exit(1);
  186.         };
  187.  
  188.     offset = read_LONG(0L);
  189.     fseek(from.fp, offset, SEEK_SET);
  190.     read_IFD();
  191.  
  192.     if(from.Intel) puts("Intel形式です。");
  193.               else puts("モトローラ形式です。");
  194.  
  195.     printf("%8lu : New_Subfile Type\n",from.New_Subfile_Type);
  196.     printf("%8u : Image Width\n",from.Image_Width);
  197.     printf("%8u : Image Length\n",from.Image_Length);
  198.     printf("%8u : Bits Per Pixel",from.Bits_Per_Sample);
  199.     if(from.Opacity) puts("(with opacity)"); else puts("");
  200.     printf("%8u : Compression\n",from.Compression);
  201.     printf("%8u : Photo Interp\n",from.Photo_Interp);
  202.     printf("%8u : Fill order\n",from.Fill_Order);
  203.     if( from.Strip_Offset == NULL )
  204.       puts("-------- : Strip Offset");
  205.     else
  206.       printf("%08lx : Strip Offset\n",*from.Strip_Offset);
  207.     printf("%8u : Samples Per Pixel\n",from.Samples_Per_Pixel);
  208.     printf("%8lu : Rows Per Strip\n",from.Rows_Per_Strip);
  209.     if( from.Strips_ByteCount == NULL )
  210.       puts("-------- : Strips ByteCount");
  211.     else
  212.       printf("%8lx : Strips ByteCount\n",*from.Strips_ByteCount);
  213.     printf("%8u : Max Sample Value\n",from.Max_Sample_Value);
  214.     printf("%4lu/%4lu: X Resolution\n",from.X_Resolution.bunsi,from.X_Resolution.bunbo);
  215.     printf("%4lu/%4lu: Y Resolution\n",from.Y_Resolution.bunsi,from.Y_Resolution.bunbo);
  216.     printf("%8u : Planer Config\n",from.Planer_Config);
  217.     printf("%8d : Color Map\n",from.Color_Map);
  218.     
  219. /*    for(i=0; i<from.Strips; i++)
  220.         printf("Strip %d: offset %08lx  length %08x\n",
  221.             i,*(from.Strip_Offset+i),*(from.Strips_ByteCount+i) ); */
  222.  
  223.     /* 今のところ、JPEG等には非対応 */
  224.     if( ( from.Compression != 1 ) && ( from.Compression !=5 ) ){
  225.         fputs("\nLZW圧縮以外の圧縮には、対応してません。\n",stderr);
  226.         exit(1);
  227.         };
  228.  
  229.     /* 書き込みファイルの属性決定 */
  230.     if( Intel == 2 ) to.Intel = from.Intel;
  231.                 else to.Intel = Intel;
  232.     to.Opacity = 0;
  233.     to.Strips = 0;
  234.     to.New_Subfile_Type = 0;
  235.     to.Image_Width = from.Image_Width;
  236.     if(expand) to.Image_Width += from.Image_Width / 3;
  237.     to.Image_Length = from.Image_Length;
  238.     if(to.Bits_Per_Sample == 0) to.Bits_Per_Sample = from.Bits_Per_Sample;
  239.     switch(to.Bits_Per_Sample){
  240.         case  8:
  241.             to.Photo_Interp = 3;
  242.             to.Samples_Per_Pixel = 1;
  243.             break;
  244.         case 16:
  245.             to.Photo_Interp = 1;
  246.             to.Samples_Per_Pixel = 1;
  247.             break;
  248.         case 24:
  249.             to.Photo_Interp = 2;
  250.             to.Samples_Per_Pixel = 3;
  251.             }
  252.     to.Fill_Order = 1;
  253.  
  254.     if( to.Bits_Per_Sample == 16 )
  255.         to.Max_Sample_Value = (1<<15)-1;
  256.     else
  257.         to.Max_Sample_Value = (1<<to.Bits_Per_Sample)-1;
  258.  
  259.     /* 解像度のチェックをしないと、NeXTなどで誤動作をまねきます */
  260.     if( to.X_Resolution.bunsi == 0 || to.X_Resolution.bunbo == 0 )
  261.         if( from.X_Resolution.bunsi && from.X_Resolution.bunbo ){
  262.             to.X_Resolution.bunsi = from.X_Resolution.bunsi;
  263.             to.X_Resolution.bunbo = from.X_Resolution.bunbo;
  264.             }
  265.         else{
  266.             to.X_Resolution.bunsi = 72UL;
  267.             to.X_Resolution.bunbo = 1UL;
  268.             }
  269.  
  270.     if( to.Y_Resolution.bunsi == 0 || to.Y_Resolution.bunbo == 0 )
  271.         if( from.Y_Resolution.bunsi && from.Y_Resolution.bunbo ){
  272.             to.Y_Resolution.bunsi = from.Y_Resolution.bunsi;
  273.             to.Y_Resolution.bunbo = from.Y_Resolution.bunbo;
  274.             }
  275.         else{
  276.             to.Y_Resolution.bunsi = 75UL;
  277.             to.Y_Resolution.bunbo = 1UL;
  278.             }
  279.  
  280.     to.Planer_Config = 1;
  281.     to.Color_Map = 0;
  282.  
  283.     if( to.Rows_Per_Strip == 0 )
  284.       /* Townsの16ビットはストライプ分割を認めない、意固地だそうな */
  285.       if( ( to.Compression==1)||( to.Bits_Per_Sample == 16 ) )
  286.         to.Rows_Per_Strip = to.Image_Length;
  287.       else
  288.         to.Rows_Per_Strip = (TEMP_SIZE)/(to.Image_Width*to.Bits_Per_Sample/8L);
  289.  
  290.     j = (to.Image_Length / to.Rows_Per_Strip) + 1;
  291.     to.Strips_ByteCount=MALLOC(4*j);
  292.     to.Strip_Offset=MALLOC(4*j);
  293.  
  294.     printf("\n[ %s ] → [ %s(%s) ] の変換です。\n",argv[from_file],ren,string[1]);
  295.     puts("変換先のファイルの書式は");
  296.     if( to.Compression == 5 ) fputs("LZW圧縮/",stdout);
  297.     printf("%dビット/1ストライプ %d ライン/",to.Bits_Per_Sample, to.Rows_Per_Strip);
  298.     if(expand) fputs("幅拡張/",stdout);
  299.     if(to.Intel == 0 ) puts("モトローラ式");
  300.                   else puts("インテル式");
  301.  
  302.     if( stop ){
  303.         puts("です。リターンで開始します。");
  304.         fgetc(stdin);
  305.         }
  306.     else puts("です。");
  307.  
  308.     if( NULL == (to.fp = fopen(string[1],"wb")) ){
  309.         fprintf(stderr,"ファイル %s の作成に失敗しました。\n",string[1]);
  310.         exit(1);
  311.         };
  312.  
  313.     if(to.Intel) write_SHORT(0UL,0x4949);    /* intel形式 */
  314.             else write_SHORT(0UL,0x4d4d);    /* モトローラ形式 */
  315.  
  316.     write_SHORT(0UL,42);    /* バージョン */
  317.     write_LONG(0UL,0UL);    /* IFD offset のダミー値 */
  318.  
  319.     /* さて、グラフィックデータの書き込みっと・・・ */
  320.     switch(from.Bits_Per_Sample){
  321.         case 8 : change8(expand) ; break;
  322.         case 12: change12(expand); break;
  323.         case 16: change16(expand); break;
  324.         case 24: change24(expand); break;
  325.         case 32: change32(expand); break;
  326.         default: fputs("この変換には対応してません。\n",stderr);
  327.                  exit(1);
  328.         };
  329.  
  330.     /* 残るはIFDだ! */
  331.     puts("IFD を書き込みます。");
  332.     write_IFD();
  333.  
  334.     puts("変換は終了しました。");
  335.     fclose(to.fp);
  336.  
  337.     if( NULL == fopen( ren,"rb") ) rename(string[1],ren);
  338.     else printf("Rename できなかったので、[ %s ]のまま残します。\n",string[1]);
  339. }
  340.